//
//  ViewController.m
//  CALayerTest
//
//  Created by ficow on 4/14/16.
//  Copyright © 2016 ficow. All rights reserved.
//

#import "ViewController.h"

#define WeakSelf __weak __typeof(self) weakSelf = self
#define StrongSelf __strong __typeof(weakSelf) self = weakSelf

@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self ficowCustomView];
}

- (void)ficowCustomView{
    UIView *magnetaView = [[UIView alloc]initWithFrame:self.view.bounds];
    magnetaView.backgroundColor = [UIColor magentaColor];
    [self.view addSubview:magnetaView];
    
    UIView *grayView = [[UIView alloc]initWithFrame:self.view.bounds];
    grayView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:grayView];
    NSArray *bnTitleArray = @[@"添加",@"翻页",@"移入",@"揭开",@"立方体",@"收缩",@"翻转",@"水波"];
    NSMutableArray *bnArray = [[NSMutableArray alloc]init];
    CGFloat totalHeight = [UIScreen mainScreen].bounds.size.height;
    for (int i = 0; i < 8; i++) {
        UIButton *bn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [bn setTitle:[bnTitleArray objectAtIndex:i] forState:UIControlStateNormal];
        NSInteger row = i / 4;
        NSInteger col = i % 4;
        bn.frame = CGRectMake(5 + col * 80, totalHeight - (2 - row) * 45 - 20, 70, 35);
        [self.view addSubview:bn];
        [bnArray addObject:bn];
    }
    [bnArray[0] addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[1] addTarget:self action:@selector(curl:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[2] addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[3] addTarget:self action:@selector(reveal:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[4] addTarget:self action:@selector(cube:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[5] addTarget:self action:@selector(suck:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[6] addTarget:self action:@selector(oglFlip:) forControlEvents:UIControlEventTouchUpInside];
    [bnArray[7] addTarget:self action:@selector(ripple:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

//添加
- (void)add:(id)sender {
    [UIView animateWithDuration:1 animations:^{
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    }];
}
//翻页
- (void)curl:(id)sender {
    
    WeakSelf;
    [UIView animateWithDuration:1 animations:^{
        StrongSelf;
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];
    }];
}
//移入
- (void)move:(id)sender {
    CATransition *trans = [CATransition animation];
    trans.duration = 2.0f;
    trans.type = kCATransitionMoveIn;
    trans.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:trans forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
//揭开
- (void)reveal:(id)sender {
    CATransition *trans = [CATransition animation];
    trans.duration = 2.0f;
    trans.type = kCATransitionReveal;
    trans.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:trans forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
//立方体
- (void)cube:(id)sender {
    CATransition *trans = [CATransition animation];
    trans.duration = 2.0f;
    trans.type = @"cube";
    trans.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:trans forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
//收缩
- (void)suck:(id)sender {
    CATransition *trans = [CATransition animation];
    trans.duration = 2.0f;
    trans.type = @"suckEffect";
    [self.view.layer addAnimation:trans forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
//翻转
- (void)oglFlip:(id)sender {
    CATransition *trans = [CATransition animation];
    trans.duration = 2.0f;
    trans.type = @"oglFlip";
    trans.subtype = kCATransitionFromBottom;
    [self.view.layer addAnimation:trans forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}
//水波
- (void)ripple:(id)sender {
    CATransition *trans = [CATransition animation];
    trans.duration = 2.0f;
    trans.type = @"rippleEffect";
    [self.view.layer addAnimation:trans forKey:@"animation"];
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
}

@end
